<template>
  <view class="page">
    <!-- #ifndef H5-DINGDING -->
    <theme-navbar
      :is-white="false" :title="navTitle" :custom-back="goBack"
      style="z-index: 99999"
    ></theme-navbar>
    <!-- #endif -->
    <view></view>
    <view class="content">
      <u-form
        ref="uForm" v-model="formData" label-position="top"
        :rules="rules" label-width="auto" :model="formData"
        :errorType="errorType"
      >
        <form-section title="基础信息" v-if="!readonly"></form-section>
        <u-form-item label="案件编号" :required="true" prop="bizCode">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.bizCode"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请单位">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyOrgName"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请人">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyPersonName"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请日期">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyTime"
          ></u-input>
        </u-form-item>
        <u-form-item label="申请原因">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.applyReason"
          ></u-input>
        </u-form-item>
        <form-section title="审批信息" style="margin-top: 35rpx" v-if="!readonly"></form-section>
        <u-form-item label="审核人">
          <u-input
            placeHolder="" type="text" :disabled="true"
            v-model="formData.approvePersonName"
          ></u-input>
        </u-form-item>
        <u-form-item label="审核意见" :required="true" prop="remark">
          <u-input
            placeHolder="" type="text" :disabled="readonly||!isEnd"
            v-model="formData.remark"
          ></u-input>
        </u-form-item>
        <u-form-item label="分管领导审批意见" v-if="ifReview">
          <u-input
            placeHolder="" type="text" :disabled="isEnd"
            v-model="formData.opinionsLeaders"
          ></u-input>
        </u-form-item>
        <u-form-item
          label="查阅期限" prop="startDate" :required="true"
          ref="startChangeRef"
        >
          <view style="display: flex; flex-direction: row; align-items: center; width: 100%">
            <u-input
              v-model="formData.startDate" :disabled="readonly" @click="ifShowStart=!ifShowStart&&!readonly"
              style="flex: 1" placeholder="请选择日期"
            />
            <view style="margin-left: 10rpx; margin-right: 10rpx; flex-shrink: 0">
              至
            </view>
            <u-input
              v-model="formData.endDate" :disabled="readonly" @click="ifShowEnd=!ifShowEnd&&!readonly"
              style="flex: 1" placeholder="请选择日期"
            />
          </view>
        </u-form-item>
      </u-form>
    </view>
    <view class="submit-btns btn-other-cls">
      <u-button
        class="u-btn" type="primary" v-show="!readonly"
        @click="isAgree(AgreeDict.AGREE)"
      >
        同意
      </u-button>
      <u-button
        class="u-btn" type="primary" v-show="!readonly"
        @click="isAgree(AgreeDict.NOT_AGREE)"
      >
        不同意
      </u-button>
    </view>
    <!-- 时间选择器 -->
    <u-calendar
      mode="range" max-date="2050-01-01" v-model="ifShowStart"
      @change="changeDate('startDate',$event)"
    ></u-calendar>
    <u-calendar
      mode="range" max-date="2050-01-01" v-model="ifShowEnd"
      @change="changeDate('endDate',$event)"
    ></u-calendar>
  </view>
</template>
<script>
import DicChoice from '@/components/dic_choice.vue';
import DicInput from '@/components/dic_input.vue';
import commonService from '@/service/common/common.service';
import checkService from '@/service/check/check.service.js';
import FormSection from '@/components/form-section.vue';
import { L_ARCHIVE_APPROVE_STATUS, } from '@/service/common/mapping/enum.js';
import { commonMixin, } from '@/config/commonMixin.js';

export default {
  components: {
    DicChoice,
    DicInput,
    FormSection,
  },
  mixins: [commonMixin,],
  data() {
    return {
      formData: {},
      rightIconStyle: {
        fontSize: '40rpx',
      },
      ifReview: false,
      isEnd: false,
      user: this.$storage.get(this.$rp.common.USER),
      prevParam: {},
      operateType: 'approve',
      ifShowStart: false,
      ifShowEnd: false,
      readonly: false,
      errorType: ['border-bottom', 'message',],
      rules: {
        bizCode: [
          {
            required: true,
            message: '请输入案件编号',
            // 可以单个或者同时写两个触发验证方式
            trigger: 'blur,change',
          },
        ],
        remark: [
          {
            required: true,
            message: '请输入审核意见',
            trigger: 'blur,change',
          },
        ],
        startDate: [
          {
            required: true,
            message: '请选择查阅期限',
            trigger: 'blur,change',
          },
        ],
      },
      AgreeDict: {},
    };
  },
  onLoad() {
    this.AgreeDict = L_ARCHIVE_APPROVE_STATUS;
    console.log('L_ARCHIVE_APPROVE_STATUS', L_ARCHIVE_APPROVE_STATUS);
    let opt = this.getRouteParam() || {};
    this.prevParam = opt;
    this.init();
    console.log(opt, 'opt');
    if (this.user.orgLevel === 1) {
      this.formData.applyOrgName = this.user.provOrgName;
      // this.baseInfo.orgUuid = this.user.provOrgUuid;
    } else {
      this.formData.applyOrgName = this.user.townOrgName;
      // this.baseInfo.orgUuid = this.user.townOrgUuid;
    }
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    init() {
      this.$modal.showLoading('加载中...');
      // debugger;
      commonService
        .getBizTaskConfigBybizCode({
          taskId: this.prevParam.taskUuid,
          bizCode: this.prevParam.bizCode,
          bizUuid: this.prevParam.bizUuid,
          processDefKey: this.prevParam.processDefKey,
          operateType: this.prevParam.operateType,
          sysCode: this.prevParam.sysCode,
          menuCode: '',
        })
        .then((res) => {
          this.$modal.hideLoading();
          if (res.success) {
            this.initList();
          } else {
            this.$u.toast(res.msg);
            this.error();
          }
        })
        .catch((e) => {
          // 中转失败
          this.$modal.hideLoading();
          this.$modal.alert('卷宗打开失败');
        });
    },

    initList() {
      // 举报卷宗
      if (this.prevParam.bizCode == 'reportFileReview') {
        commonService
          .getArchivesApproveList({
            menuCode: '',
            applyId: this.prevParam.bizUuid,
            page: 1,
            limit: 15,
            bizType: '901007',
          })
          .then((res) => {
            if (res.success) {
              // debugger;
              this.formData = res.data[0];
              if (this.formData.remark == undefined) {
                this.formData.remark = '同意';
              }
              this.getFlowNode();
            }
          })
          .catch((err) => {
            console.log('err:', err);
          });
      } else if (this.prevParam.bizCode == 'caseFileReview') {
        this.ifReview = true;
        console.log('案件卷宗');
        commonService
          .getCaseApplyList({
            applyId: this.prevParam.bizUuid,
            menuCode: '',
          })
          .then((res) => {
            if (res.success) {
              this.getFlowNode();
              this.getCaseList();
            }
          })
          .catch((err) => {
            console.log('err:', err);
          });
      }
    },

    // caseFileReview 案件卷宗
    getCaseList() {
      commonService
        .getArchivesApproveList({
          menuCode: '',
          applyId: this.prevParam.bizUuid,
          page: 1,
          limit: 15,
          bizType: '901002',
        })
        .then((res) => {
          if (res.success) {
            this.getFlowIsFinish();
            if (res.data.length == 0) {
              this.getCaseApproveList();
            } else {
              this.formData = res.data[0];
            }
            if (this.formData.remark == undefined) {
              this.formData.remark = '同意';
            }
          }
        })
        .catch((err) => {
          console.log('err:', err);
        });
    },

    // 判断几级审批
    getFlowIsFinish() {
      commonService
        .getFlowIsFinish({
          bizUuid: this.prevParam.bizUuid,
          noCheckPersonNodes: this.prevParam.bizCode,
        })
        .then((res) => {
          if (res.success) {
            if (res.bean) {
              // false二级审批 给下一个审批人  ture审批结束
              // this.updateBackState();
              this.formData.opinionsLeaders = '同意';
              console.log(this.formData.opinionsLeaders, 'this.formData.opinionsLeaders');
              this.isEnd = false;
            } else {
              this.isEnd = true;
            }
          }
        })
        .catch((err) => {
          console.log('err:', err);
        });
    },

    // 二级审批
    getCaseApproveList() {
      commonService
        .getCaseApproveList({
          menuCode: '',
          applyId: this.prevParam.bizUuid,
          page: 1,
          limit: 15,
          bizType: '901002',
        })
        .then((res) => {
          if (res.success) {
            // this.getCaseList();
            this.formData = res.data[0];
          }
        })
        .catch((err) => {
          console.log('err:', err);
        });
    },

    // 获取审批节点信息
    getFlowNode() {
      checkService
        .getFlowNodeStep({
          menuCode: '',
          bizUuid: this.prevParam.bizUuid,
          processDefKey: this.prevParam.bizCode,
        })
        .then((res) => {
          if (res.success && res.bean.step == 'view') {
            this.readonly = true;
          }
        })
        .catch((err) => {});
    },

    // 日期选择
    changeDate(type, e) {
      console.log('e:', type, e);
      this.formData.startDate = e.startDate;
      this.formData.endDate = e.endDate;
    },

    isAgree(i) {
      this.formData.isAgree = i;
      console.log('i', this.formData.startDate, this.isEnd, i);

      this.$refs.uForm.validate((valid) => {
        if (valid) {
          if (this.isEnd && this.formData.isAgree == '02') {
            //  不是结束节点 选择审批人
            this.$u.route({
              url: '/pages/commonPage/flowUserSelectComponent',
              params: {
                flowFormCode: this.prevParam.bizCode,
                docInsUuid: this.prevParam.bizUuid,
                page: 1,
                limit: 15,
              },
            });
          } else {
            console.log('999');
            this.ifAgree();
          }
        }
      });
    },

    updateBackState(params) {
      let nextPersonUuids = params.flowUserSelectBack
        .map((item) => {
          return item.personId;
        })
        .join(',');
      console.log(nextPersonUuids, params, '000');
      this.formData['nextPersonUuids'] = nextPersonUuids;
      this.ifAgree();
    },

    // 是否同意接口封装
    ifAgree() {
      commonService
        .saveArchives(this.formData)
        .then((res) => {
          if (res.success) {
            this.$modal.alert(res.msg);
            this.backPrev({
              paramKey: 'refresh',
              param: true,
            });
          }
        })
        .catch((err) => {});
    },
  },
};
</script>
<style scoped lang="scss">
	.page {
		padding: 0;
	}

	.sub-title {
		background: #f5f5f5;
		color: #989ea1;
		font-size: 24rpx;
		border-radius: 4px;
		margin-right: 10px;
		padding: 4px 8px;
	}

	.f-ctn {
		width: 100%;
	}

	.tb-lay {
		width: 900px;
		margin-left: 1px;
		margin-right: 1px;
	}

	.content {
		padding-left: $cm-content-paddng-row;
		padding-right: $cm-content-paddng-row;
		padding-top: $cm-conent-padding-column;
		padding-bottom: 70px;
	}

	/deep/.u-td {
		height: auto;
	}
</style>
